package binPack
{
	import flash.geom.Rectangle;

	public class DisjointRectCollection
	{
		public var rects:Vector.<Rectangle> = new Vector.<Rectangle>();
		public function DisjointRectCollection()
		{
		}
		
		public function Add(r:Rectangle):Boolean{
			// Degenerate rectangles are ignored.
			if (r.width == 0 || r.height == 0)
				return true;
			
			if (!Disjoint(r))
				return false;
			rects.push(r);
			return true;
		}
		
		public function Clear():void{
			while(this.rects.length){
				this.rects.pop();
			}
		}
		
		public function Disjoint(r:Rectangle):Boolean {
			// Degenerate rectangles are ignored.
			if (r.width == 0 || r.height == 0)
				return true;
			
			for(var i:int = 0; i < rects.length; ++i)
				if (!DisjointRectCollection.Disjoint(rects[i], r))
					return false;
			return true;
		}
		
		public static function Disjoint(a:Rectangle, b:Rectangle):Boolean{
			if (a.x + a.width <= b.x ||
				b.x + b.width <= a.x ||
				a.y + a.height <= b.y ||
				b.y + b.height <= a.y)
				return true;
			return false;
		}
	}
}